﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using BySoft.Server.Business;
using System.Text;
using BySoft.Server.Common;
using BySoft.Web.Utility;
using BySoft.Web.Site;

namespace BySoft.Web.Site
{
    public partial class Main : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitData();
                InitMainOrderExpire();
            }
        }

        private void InitMainOrderExpire()
        {
            divMainOrderExpire.InnerHtml = "";
            DataTable dt = ClsComm.GetMainWorkExpire(CurrentUser.UserID);
            if (dt == null || dt.Rows.Count < 1)
            {
                return;
            }
            StringBuilder sb = new StringBuilder();
            foreach (DataRow item in dt.Rows)
            {
                string strlx = CommonMethod.GetString(item["lx"]);
                string strWorkOrderID = CommonMethod.GetString(item["WorkOrderID"]);
                string str = "";
                if(strlx.Equals("1"))
                {
                    str = string.Format("<span class=\"divMainRightBook\">工作订单：[{0}],预约日期到了，请添加收款单！</span><br />", strWorkOrderID);
                }
                else if (strlx.Equals("2"))
                {
                    str = string.Format("<span class=\"divMainRightContract\">工作订单：[{0}],合约日期到了，请添加收款单！</span><br />", strWorkOrderID);
                }
                else if (strlx.Equals("3"))
                {
                    str = string.Format("<span class=\"divMainRightCheck\">工作订单：[{0}],验收日期到了，请添加收款单！</span><br />", strWorkOrderID);
                }
                else if (strlx.Equals("4"))
                {
                    str = string.Format("<span class=\"divMainRightFix\">工作订单：[{0}],保固日期到了，请添加收款单！</span><br />", strWorkOrderID);
                }
                sb.AppendLine(str);
            }
            divMainOrderExpire.InnerHtml = sb.ToString();
        }



        private void InitData()
        {
            divMainLeft.Visible = false;
            divMainLeft.InnerHtml = "";
            divMainLeftBillOnein.InnerHtml = "";
            DataSet ds = ClsComm.GetMainSet(CurrentUser.UserID);
            DataTable dt1 = ds.Tables[0];
            DataTable dt2 = ds.Tables[1];
            int icount = 0;
            InitDataTotal(dt1, out icount);
            if (icount > 0)
            {
                divMainLeft.Visible = true;
                InitDataBillTotal(dt2);
            }
            else
            {
                divMainLeft.Visible = false;
            }
        }

        private void InitDataTotal(DataTable dt, out int icount)
        {
            icount = 0;
            if (dt == null || dt.Rows.Count < 1)
            {
                return;
            }
            icount = CommonMethod.ConvertToInt(dt.Rows[0]["TotalBillNum"]);
            if (icount > 0)
            {
                lblTotalBillNum.Text = string.Format("共有{0}个单据未审核", icount);
            }
            else
            {
                lblTotalBillNum.Text = "";
            }
        }

        private void InitDataBillTotal(DataTable dt)
        {
            if (dt == null || dt.Rows.Count < 1)
            {
                return;
            }

            string str = "<table class=\"divMainLeftBillTotaltable\">";
            int i = 1;
            int imax = 3;
            foreach (DataRow item in dt.Rows)
            {
                if(i==1)
                {
                    str += "<tr class=\"divMainLeftBillTotaltr\">";
                    str += GetInitDataBillTotal(item);
                }
                else if (i == imax)
                {
                    str += GetInitDataBillTotal(item);
                    str += "</tr>";
                }
                else
                {
                    str += GetInitDataBillTotal(item);
                }
                i++;
                if (i > imax)
                {
                    i = 1;
                }
            }
            if (i < imax+1)
            {
                for (int j = i; j < imax + 1; j++)
                {
                    str += "<td class=\"divMainLeftBillTotaltd\"></td>";
                }
                str += "</tr>";
            }
            str += "</table>";
            divMainLeft.InnerHtml = str;
        }

        private string GetInitDataBillTotal(DataRow item)
        {
            string strBillType = CommonMethod.GetString(item["BillType"]);
            string strBillTypeText = GetBillTypeText(strBillType);
            string strBillNum = CommonMethod.GetString(item["BillNum"]);
            return string.Format("<td class=\"divMainLeftBillTotaltd\"><span onclick='ShowMsg(\"{0}\");'>{1}[{2}]</span></td>", strBillType, strBillTypeText, strBillNum);
        }

        private string GetBillTypeText(string strBillType)
        {
            string str = "";
            strBillType = strBillType.ToUpper();
            switch (strBillType)
            {
                case "BS_WORKORDER": str = "工作订单"; break;
                case "BS_QUOTE": str = "报价单"; break;
                case "CG_PURCHASE": str = "采购单"; break;
                case "CG_APPLYPUR": str = "请购单"; break;
                case "CG_PURINSTOCK": str = "进料检验单"; break;
                case "BS_SALELIST": str = "售货清单"; break;
                case "BS_TESTVEHICLE": str = "试车单"; break;
                case "BS_EXTERNALACCEPT": str = "外部验收单"; break;
                case "BS_REPAIRSTATUS": str = "維修狀況表"; break;
                case "BS_OUTGOOD": str = "出货清单"; break;
                case "BS_REPAIRQUOTE": str = "维修报价单"; break;
                case "BS_REPAIRCOLLECT": str = "维修收款单"; break;
                case "BS_INNERACCEPT": str = "机台内部验收单"; break;
                case "CG_SCRAPPARTS": str = "报废单"; break;
                case "BM_BOM": str = "产品Bom"; break;
                case "CG_PURRETURN": str = "进料退货单"; break;
                case "CG_OTHERINSTOCK": str = "其他入库单"; break;
                case "CG_OTHEROUTSTOCK": str = "其他出库单"; break;
                case "BM_SENDMATPLAN": str = "发料计划"; break;
                case "BM_SYSTEMSEND": str = "系统发料单"; break;
                case "CG_PRODUCEINSTOCK": str = "入库单"; break;
                case "CG_REQUISITION": str = "领料单"; break;
                case "BS_HOURRECORD": str = "工时记录表"; break;
                case "BS_WORKORDERCOLLECT": str = "收款单"; break;
                case "BS_CROSSAREAAPPLY": str = "跨区申请单"; break;
                default:
                    break;
            }
            return str;
        }

        private string GetBillURL(string strBillType, string strBillID)
        {
            string str = "";
            strBillType = strBillType.ToUpper();
            switch (strBillType)
            {
                case "BS_WORKORDER": str = "Business/WorkOrderList.aspx?id=" + strBillID; break;
                case "BS_QUOTE": str = "Business/QuoteList.aspx?id=" + strBillID; break;
                case "CG_PURCHASE": str = "Technology/PurchaseList.aspx?id=" + strBillID; break;
                case "CG_APPLYPUR": str = "Technology/RequestedBuyList.aspx?id=" + strBillID; break;
                case "CG_PURINSTOCK": str = "StorageManage/InStockList.aspx?id=" + strBillID; break;
                case "BS_SALELIST": str = "SalesData/SaleListList.aspx?id=" + strBillID; break;
                case "BS_TESTVEHICLE": str = "SalesData/TestVehicleList.aspx?id=" + strBillID; break;
                case "BS_EXTERNALACCEPT": str = "SalesData/ExternalAcceptList.aspx?id=" + strBillID; break;
                case "BS_REPAIRSTATUS": str = "SalesData/RepairStatusList.aspx?id=" + strBillID; break;
                case "BS_OUTGOOD": str = "SalesData/OutGoodList.aspx?id=" + strBillID; break;
                case "BS_REPAIRQUOTE": str = "SalesData/RepairQuoteList.aspx?id=" + strBillID; break;
                case "BS_REPAIRCOLLECT": str = "SalesData/RepairCollectList.aspx?id=" + strBillID; break;
                case "BS_INNERACCEPT": str = "SalesData/InnerAcceptList.aspx?id=" + strBillID; break;
                case "CG_SCRAPPARTS": str = "StorageManage/ScrapPartsList.aspx?id=" + strBillID; break;
                case "BM_BOM": str = "Bom/MDBomList.aspx?id=" + strBillID; break;
                case "CG_PURRETURN": str = "StorageManage/ReturnPartsList.aspx?id=" + strBillID; break;
                case "CG_OTHERINSTOCK": str = "StorageManage/OtherInStockList.aspx?id=" + strBillID; break;
                case "CG_OTHEROUTSTOCK": str = "StorageManage/OtherOutStockList.aspx?id=" + strBillID; break;
                case "BM_SENDMATPLAN": str = "Technology/SendMaterialPlanList.aspx?id=" + strBillID; break;
                case "BM_SYSTEMSEND": str = "Technology/SystemIssueList.aspx?id=" + strBillID; break;
                case "CG_PRODUCEINSTOCK": str = "Production/InStockList.aspx?id=" + strBillID; break;
                case "CG_REQUISITION": str = "Production/PickingList.aspx?id=" + strBillID; break;
                case "BS_HOURRECORD": str = "SalesData/HourRecordList.aspx?id=" + strBillID; break;
                case "BS_WORKORDERCOLLECT": str = "Business/WorkOrderCollectList.aspx?id=" + strBillID; break;
                case "BS_CROSSAREAAPPLY": str = "Business/CrossAreaApplyList.aspx?id=" + strBillID; break;
                default:
                    break;
            }
            return str;
        }

        protected void btnBillType_Click(object sender,EventArgs e)
        {
            string strBillType = hdBillType.Value;
            DataTable dt = ClsComm.GetMainBill(strBillType);
            if (dt == null || dt.Rows.Count < 1)
            {
                UIHelper.AlertMessage(this.UpdatePanel1, "没有找到数据，请联系管理员！");
                return;
            }

            lblMainLeftBillOnein.Text = GetBillTypeText(strBillType);
            string str = "<ul>";
            foreach (DataRow item in dt.Rows)
            {
                str += string.Format("<li class=\"divMainLeftBillOneli\" onclick='switchbill(\"{1}\",\"{0}\")'>{0}</li>", CommonMethod.GetString(item["BillID"]), CommonMethod.GetString(item["BillType"]));
            }
            str += "</ul>";
            divMainLeftBillOnein.InnerHtml = str;
        }

        protected void btnBillID_Click(object sender, EventArgs e)
        {
            Response.Redirect(GetBillURL(hdBillType.Value,hdBillID.Value));
        }
    }
}